//
//  ReadViewController.m
//  Slice
//
//  Created by lanou on 16/6/13.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "ReadViewController.h"
#import "ListTableViewController.h"
@interface ReadViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation ReadViewController

- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
   return  _dataArray;
}


//按钮动作
- (IBAction)open:(id)sender {
    [(AppDelegate *)([UIApplication sharedApplication].delegate) action];
}
//创建 CollectionView
- (void)creatCollectionView {
    UICollectionViewFlowLayout *layOut = [[UICollectionViewFlowLayout alloc] init];
    layOut.minimumLineSpacing = 5;
    layOut.itemSize = CGSizeMake((kWidth - 10) / 3, (kWidth - 10) / 3);
    layOut.minimumInteritemSpacing = 5;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, kHeight - kWidth, kWidth, kWidth) collectionViewLayout:layOut];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
}

//请求数据
- (void)getData {
    [RequestTool requsetWithType:GET URLString:kReadURL paremeter:nil callBack:^(NSData *data, NSError *error) {
//       json 数据解析
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSArray *listArr = jsonDic[@"data"][@"list"];
        for (NSDictionary *dic1 in listArr) {
           ReadTypeModel *model = [ReadTypeModel modelWithDiction:dic1];
            [self.dataArray addObject:model];
        }
//       回主线程刷新数据
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
            self.cououseArray = jsonDic[@"data"][@"carousel"];
//           获取轮播图地址
            NSMutableArray *arrm = [NSMutableArray array];
            for (NSDictionary *dic3 in self.cououseArray) {
                [arrm addObject:dic3[@"img"]];
            }
            [self creatCourousWith:arrm];
        });
    }];
}

//创建轮播图
- (void)creatCourousWith:(NSArray *)imgs {
    CoueousPic *cour = [[CoueousPic alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight - kWidth - 70) images:imgs];
    [self.view addSubview: cour];
//    轮播图点击
    cour.imgClickBlock = ^(NSInteger index) {

    };
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    [self creatCollectionView];
//    如果控制器存在导航栏,系统会将 ScrollView 的内容往下偏移 64
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
//    从 xib 注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"ReadCllectionCell" bundle:nil] forCellWithReuseIdentifier:@"readCell"];
    
    
}


#pragma mark -----collectionView 代理方法------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BaseCollectionViewCell *cell = [CollectionFactory cellWithCollectionView:collectionView indentifier:@"readCell" indexPath:indexPath];
    [cell setContentWithModel:self.dataArray[indexPath.row]];
    
    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ListTableViewController *listVC = [[ListTableViewController alloc] init];
    ReadTypeModel *model = self.dataArray[indexPath.row];
    listVC.typeId = model.type;
    [self.navigationController pushViewController:listVC animated:YES];
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
